package net.thomaswilburn.cobweb;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class browser extends Activity {
    /** Called when the activity is first created. */
	WebView webview;
	URLUtil urlUtil;
	EditText urlEdit;
	String lastURL;
	
    @Override
    public void onCreate(Bundle snowflake) {
        super.onCreate(snowflake);
        lastURL = "www.google.com";
        if (snowflake != null) {
        	Log.w("cobweb", snowflake.getString("lastURL"));
        	lastURL = snowflake.getString("lastURL");
        }
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, 0);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new CobwebViewClient());
        webview.setWebChromeClient(new CobwebChromeClient());
        webview.getSettings().setUserAgentString("none");
        webview.getSettings().setJavaScriptEnabled(false);
        webview.getSettings().setSaveFormData(false);
        webview.getSettings().setSavePassword(false);
        //webview.getSettings().setCacheMode(0);
        urlEdit = (EditText) findViewById(R.id.UrlEditText);
        hideBar();
        urlEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (v.getId() == R.id.UrlEditText && !hasFocus) {
					hideBar();
				}
			}
        	
        });
        urlEdit.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				if (arg1 == KeyEvent.KEYCODE_ENTER) {
					String userURL = ((EditText) arg0).getText().toString();
					lastURL = URLUtil.guessUrl(userURL);
					webview.loadUrl(URLUtil.guessUrl(userURL));
					webview.requestFocus();
					hideBar();
					return true;
				}
				return false;
			}
        	
        });
        webview.loadUrl(URLUtil.guessUrl(lastURL));
    }
    
    @Override
    public void onConfigurationChanged(Configuration config) {
    	super.onConfigurationChanged(config);
    }
    
    @Override
    public void onSaveInstanceState(Bundle snowflake) {
    	snowflake.putString("lastURL", lastURL);
    	super.onSaveInstanceState(snowflake);
    }
    
    @Override
    public void onDestroy() {
    	webview.clearCache(true);
    	super.onDestroy();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webview.canGoBack()) {
            	webview.goBack();
                return true;
            }
        } else {
        	if (event.isPrintingKey()) triggerURLBar(event);
        	//urlEdit.setText(keyCode);
        }
        return super.onKeyDown(keyCode, event);
    }
    
    public void triggerURLBar(KeyEvent event) {
    	showBar();
    	urlEdit.dispatchKeyEvent(event);
    }
    
    public void setTitle(String title) {
    	getWindow().setTitle((CharSequence) title);
    }
    
    public void setWebProgress(int progress) {
    	getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress * 100);
    }
    
    public void showBar() {
    	urlEdit.setVisibility(View.VISIBLE);
    	urlEdit.setFocusable(true);
    	urlEdit.setText(webview.getUrl());
    	urlEdit.requestFocus();
    }
    
    public void hideBar() {
    	urlEdit.setFocusable(false);
    	urlEdit.setVisibility(View.GONE);
    }
    
    private class CobwebViewClient extends WebViewClient {
        WebView w;
        
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        	w = view;
        	lastURL = url;
            view.loadUrl(url);
            return true;
        }
        
        @Override
        public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
        	if (event.isPrintingKey()) triggerURLBar(event);
        }
    }
    
    private class CobwebChromeClient extends WebChromeClient {
    	
    	@Override
    	public void onProgressChanged(WebView view, int progress) {
    		setWebProgress(progress);
    	}
    	
    }
}